package com.atguigu.dga.governance.service.impl;

import com.atguigu.dga.governance.bean.GovernanceAssessTable;
import com.atguigu.dga.governance.bean.GovernanceType;
import com.atguigu.dga.governance.constant.GovernanceConstant;
import com.atguigu.dga.governance.mapper.GovernanceAssessTableMapper;
import com.atguigu.dga.governance.service.GovernanceAssessTableService;
import com.atguigu.dga.governance.service.GovernanceTypeService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 表治理考评情况 服务实现类
 * </p>
 *
 * @author liuxu
 * @since 2023-10-16
 */
@Service
@DS("dga")
public class GovernanceAssessTableServiceImpl extends ServiceImpl<GovernanceAssessTableMapper, GovernanceAssessTable> implements GovernanceAssessTableService {

    @Autowired
    GovernanceAssessTableMapper governanceAssessTableMapper;

    @Autowired
    GovernanceTypeService governanceTypeService ;
    @Override
    public void calcGovernanceTableScore(String assessDate) {
        //幂等处理
        remove(
                new QueryWrapper<GovernanceAssessTable>().eq("assess_date",assessDate)
        );
        //查询获取GovernanceAssessTable对象集合
        List<GovernanceAssessTable> governanceAssessTables = governanceAssessTableMapper.selectGovernanceAssessTable(assessDate);

        //获取五维权重
        List<GovernanceType> listGovernanceType = governanceTypeService.list();
        //将五维权重放入map集合中
        HashMap<String, BigDecimal> governanceTypeMap = new HashMap<>();
        for (GovernanceType governanceType : listGovernanceType) {
            governanceTypeMap.put(governanceType.getTypeCode(),governanceType.getTypeWeight());
        }

        //计算五维总分
        for (GovernanceAssessTable governanceAssessTable : governanceAssessTables) {
            BigDecimal specScore =
                    governanceAssessTable.getScoreSpecAvg()
                            .multiply(governanceTypeMap.get(GovernanceConstant.GOVERNANCE_TYPE_SPEC)
                                    .divide(BigDecimal.TEN, 1 , RoundingMode.HALF_UP));
            BigDecimal storageScore =
                    governanceAssessTable.getScoreStorageAvg()
                            .multiply(governanceTypeMap.get(GovernanceConstant.GOVERNANCE_TYPE_STORAGE)
                                    .divide(BigDecimal.TEN, 1 , RoundingMode.HALF_UP));
            BigDecimal calcScore =
                    governanceAssessTable.getScoreCalcAvg()
                            .multiply(governanceTypeMap.get(GovernanceConstant.GOVERNANCE_TYPE_CALC)
                                    .divide(BigDecimal.TEN, 1 , RoundingMode.HALF_UP));
            BigDecimal qualityScore =
                    governanceAssessTable.getScoreQualityAvg()
                            .multiply(governanceTypeMap.get(GovernanceConstant.GOVERNANCE_TYPE_QUALITY)
                                    .divide(BigDecimal.TEN, 1 , RoundingMode.HALF_UP));
            BigDecimal securityScore =
                    governanceAssessTable.getScoreSecurityAvg()
                            .multiply(governanceTypeMap.get(GovernanceConstant.GOVERNANCE_TYPE_SECURITY)
                                    .divide(BigDecimal.TEN, 1 , RoundingMode.HALF_UP));

            BigDecimal sumScore =
                    specScore.add(storageScore).add(calcScore).add(qualityScore).add(securityScore) ;

            governanceAssessTable.setScoreOnTypeWeight( sumScore );
        }
        //批写到数据库
        saveBatch(governanceAssessTables);
    }
}
